package com.hedl.practice.diyJava;

import java.util.StringJoiner;

public class JavaStringJoiner {
    public static void main(String[] args) {

        //String[] names = {"Bob","Alice","Grace"};

        /*StringBuilder sb = new StringBuilder();
        sb.append("hello ");
        for (String name : names){
            sb.append(name).append(",");
        }
        System.out.println(sb.length());

        sb.delete(sb.length()-2,sb.length());
        sb.append("!");
        System.out.println(sb.toString());
        System.out.println(sb.length());*/


        //添加逗号，然后在数组前面添加hello，然后以！号结束
        /**
         * delimiter        分隔符
         * prefix       前缀添加
         * suffix       后缀添加
         */
/*        StringJoiner sj = new StringJoiner(",","Hello ","!");   //Hello ... [,] ...!
        for (String name:names){
            sj.add(name);
        }
        System.out.println(sj.toString());  //Hello Bob,Alice,Grace!*/



        //小练习
        String[] fields = {"name","position", "salary"};
        String table = "employee";
        String select = buildSelectSql(table,fields);
        System.out.println(select);
        System.out.println("SELECT name, position, salary FROM employee".equals(select)?"测试成功":"测试失败");
    }

    //我写的
    /*private static String buildSelectSql(String table, String[] fields) {
        String s = String.join(", ",fields);
        String columns = s.toString();
        StringBuilder sb = new StringBuilder();
            sb.append("SELECT ")
                    .append(columns)
                    .append(" FROM ")
                    .append(table);
        System.out.println(sb);
        return sb.toString();
    }*/

    private static String buildSelectSql(String table, String[] fields) {
        StringJoiner sj = new StringJoiner(", ","SELECT "," FROM "+table);
        for (String field:fields){
            sj.add(field);
        }
        return sj.toString();
    }
}
